package com.efeichong.project.service.impl;

import com.efeichong.common.PageData;
import com.efeichong.common.exception.CustomException;
import com.efeichong.common.utils.OptionEmptyFunction;
import com.efeichong.jpa.JExample;
import com.efeichong.project.domain.dao.db.SysDictDao;
import com.efeichong.project.domain.entity.db.SysDict;
import com.efeichong.project.domain.vo.DictInfoVo;
import com.efeichong.project.domain.vo.SysDictDetailVo;
import com.efeichong.project.domain.vo.SysDictVo;
import com.efeichong.project.service.SysDictService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @author
 * @date 2020/11/25
 * @description 数据字典表
 */
@RequiredArgsConstructor
@Service
public class SysDictServiceImpl implements SysDictService {

    private final SysDictDao sysDictDao;

    /**
     * 通过id查询
     *
     * @param id 主键
     * @return
     */
    @Override
    public SysDictVo getInfo(Long id) {
        return sysDictDao.selectById(id, SysDictVo.class).orElseThrow(OptionEmptyFunction.create());
    }

    /**
     * 分页查询
     *
     * @param sysDictVo
     * @return
     */
    @Override
    public PageData<SysDictVo> selectByPage(SysDictVo sysDictVo) {
        JExample<SysDict> example = new JExample();
        example.initExample(sysDictVo);
        return sysDictDao.selectByPage(example, sysDictVo.getClass());
    }

    /**
     * 新增
     *
     * @param sysDictVo
     * @return
     */
    @Transactional
    @Override
    public void insertSysDict(SysDictVo sysDictVo) {
        sysDictDao.save(sysDictVo.toPo(SysDict.class));
    }

    /**
     * 修改
     *
     * @param sysDictVo
     * @return
     */
    @Transactional
    @Override
    public void updateSysDict(SysDictVo sysDictVo) {
        if (sysDictVo.getId() == null) {
            throw new CustomException("主键为必传参数");
        }
        sysDictDao.save(sysDictVo.toPo(SysDict.class));
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public void deleteByIds(List<Long> ids) {
        sysDictDao.deleteAllByIds(ids);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Transactional
    @Override
    public void deleteById(Long id) {
        sysDictDao.deleteById(id);
    }

    /**
     * 根据类型获取数据字典详细
     *
     * @param type
     * @return
     */
    @Override
    public DictInfoVo getInfoByType(String type) {
        JExample<SysDict> example = new JExample();
        example.createCriteria().andEqualTo("type", type);
        Optional<SysDict> optional = sysDictDao.selectOne(example);
        if (optional.isPresent()) {
            DictInfoVo dictInfoVo = optional.get().toVo(DictInfoVo.class);
            List<SysDictDetailVo> dictDetailVos = dictInfoVo.getDictDetailVos();
            dictDetailVos.sort(new Comparator<SysDictDetailVo>() {
                @Override
                public int compare(SysDictDetailVo o1, SysDictDetailVo o2) {
                    return o1.getSort() - o2.getSort();
                }
            });
            return dictInfoVo;
        }

        return null;
    }
}
